<template>
  <div>
    <div class="search-container">
      <el-input v-model="keywords" placeholder="通过用户名搜索用户..." prefix-icon="el-icon-search" 
        style="width: 400px;margin-right: 10px" size="small"></el-input>
      <el-button type="primary" icon="el-icon-search" size="small" @click="doSearch">搜索</el-button>
    </div>
    <div class="admin-container">
      <el-card class="admin-card" v-for="(admin, index) in admins" :key="index">
        <div slot="header" class="clearfix">
          <span>{{admin.name}}</span>
          <el-button style="float: right; padding: 3px 0; color: red;" 
            type="text" icon="el-icon-delete" @click="deleteAdmin(admin)"></el-button>
        </div>
        <div class="userface-container">
          <img :src="admin.userFace" :alt="admin.name" :title="admin.name" class="admin-userface">
        </div>
        <div class="userinfo-container">
          <div>用户名：{{admin.name}}</div>
          <div>手机号码：{{admin.phone}}</div>
          <div>电话号码：{{admin.telphone}}</div>
          <div>地址：{{admin.address}}</div>
          <div>用户状态：
            <el-switch v-model="admin.enabled" 
              active-color="#13ce66" inactive-color="#ff4949"
              active-text="启用" inactive-text="禁用"
              @change="enabledChange(admin)">
            </el-switch>
          </div>
          <div>用户角色：
            <el-tag type="success" v-for="(role, indexj) in admin.roles" :key="indexj"
              style="margin-right: 5px;">{{role.remark}}
            </el-tag>
            <el-popover placement="right" width="300"
              title="角色列表" trigger="click" @show="showPopover(admin)" @hide="hidePopover(admin)">
              <el-select v-model="selectedRoles" multiple placeholder="请选择" style="width: 100%">
                <el-option
                  v-for="(r, index) in allRoles"
                  :key="index"
                  :label="r.remark"
                  :value="r.id">
                </el-option>
              </el-select>
              <el-button type="text" icon="el-icon-more" slot="reference"></el-button>
            </el-popover>
          </div>
          <div>备注：
            {{admin.remark}}
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'SysAdmin',
    data() {
      return {
        admins: [],
        keywords: '',
        allRoles: [],  //所有角色
        selectedRoles: []  //选中的角色
      }
    },
    methods: {
      initAdmins(){
        this.getRequest('/system/admin/?keyWords='+this.keywords).then(resp=>{
          if(resp){
            this.admins = resp.obj;
          }
        })
      },
      initAllRoles(){ //获取所有角色
        this.getRequest('/system/admin/roles').then(resp=>{
          if(resp){
            this.allRoles = resp.obj;
          }
        })
      },
      showPopover(admin){  //弹出时，类似于编辑角色的选择框
        this.initAllRoles(); 
        let roles = admin.roles; //获取已经选中的角色，这是一个对象
        this.selectedRoles = [];
        roles.forEach(r => {
          this.selectedRoles.push(r.id);
        });
      },
      hidePopover(admin){
        let flag = false;
        let myroles = [];
        Object.assign(myroles, admin.roles);
        if(myroles.length != this.selectedRoles.length){
          flag = true;
        }else{
          for(let i=0; i<myroles.length; i++){
            let role = myroles[i];
            for(let j=0; j<this.selectedRoles.length; j++){
              let sr = this.selectedRoles[j];
              if(role.id == sr){
                myroles.splice(i, 1);
                i--; //因为删除，后边的下标会-1，所以这里i--，让它匹配所以
                break;
              }
            }
          }
          if(myroles.length != 0){
            flag = true;
          }
        }
        console.log(flag)
        if(flag){
          let url = '/system/admin/updateAdminRoles?adminId=' + admin.id;
          this.selectedRoles.forEach(sr=>{
            url += '&rids=' + sr;
          })
          this.putRequest(url).then(resp => {
            if(resp){
              this.initAdmins();
            }
          })
        }
      },
      doSearch(){
        this.initAdmins()
      },
      deleteAdmin(admin){
        this.$confirm('此操作将永久删除[ '+admin.name+' ]该角色，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/system/admin/'+admin.id).then(resp=>{
            if(resp){
              // 刷新列表
              this.initAdmins();
            }
          })
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      // 更新状态
      enabledChange(admin){
        this.putRequest('/system/admin/',admin).then(resp=>{
          if(resp){
            this.initAdmins();
          }
        })
      }
    },
    // watch:{
    //   keywords(){
    //     this.initAdmins()
    //   }
    // },
    mounted(){
      this.initAdmins();
    }
  }
</script>

<style scoped>
  .search-container{
    display: flex;
    justify-content: center;
    margin-top: 10px;
  }
  .admin-container{
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    margin-top: 10px;
  }
  .userface-container{
    display: flex;
    justify-content: center;
  }
  .admin-userface{
    width: 72px;
    height: 72px;
    border-radius: 72px;
  }
  .admin-card{
    width: 420px;
    margin-top: 10px;
  }
  .userinfo-container{
    margin-top: 10px;
    font-size: 12px;
  }
</style>